package com.dyzx.hbb.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseController;
import com.dyzx.hbb.common.utils.DateFormat;
import com.dyzx.hbb.modules.sys.entity.SysAddress;
import com.dyzx.hbb.modules.sys.entity.SysOrder;
import com.dyzx.hbb.modules.sys.entity.SysOrgan;
import com.dyzx.hbb.modules.sys.entity.SysUser;
import com.dyzx.hbb.modules.sys.query.OrganQuery;
import com.dyzx.hbb.modules.sys.query.UserQuery;
import com.dyzx.hbb.modules.sys.service.SysOrganService;
import com.dyzx.hbb.modules.sys.vo.OrganUVO;
import com.dyzx.hbb.modules.sys.vo.OrganVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/sys/organ")
@RestController
@Tag(name = "03.机构管理", description = "机构相关接口")
public class OrganController extends BaseController<SysOrganService, SysOrgan, OrganQuery, OrganVO, OrganUVO> {

    @Autowired
    private SysOrganService service;



    @Override
    protected SysOrganService getBaseService() {
        return service;
    }



    @Override
    protected MyLambdaQueryWrapper<SysOrgan> queryWrapper(OrganQuery query) {
        MyLambdaQueryWrapper<SysOrgan> wrapper = new MyLambdaQueryWrapper<>();


            wrapper.select(SysOrgan::getId, SysOrgan::getName, SysOrgan::getType,
                    SysOrgan::getCode,  SysOrgan::getLeader,
                    SysOrgan::getStatus, SysOrgan::getCreateDate, SysOrgan::getUpdateDate).addressName(SysOrgan::getAddressId).as("address_name");
            wrapper.imageUrl(SysOrgan::getIcon).as("icon_url");
                   // .imageUrl(SysOrgan::getLicense).as("licenseUrl");
        if (query != null) {
            wrapper.like(StringUtils.isNotBlank(query.getName()), SysOrgan::getName, query.getName())
                    .like(StringUtils.isNotBlank(query.getLeader()), SysOrgan::getLeader, query.getLeader())
                    .dateBetween(StringUtils.isNotBlank(query.getCreateDate()), SysOrgan::getCreateDate, DateFormat.DATE, query.getCreateDate())
                    .eq(query.getType() != null, SysOrgan::getType, query.getType())
                    .eq(query.getCode() != null, SysOrgan::getCode, query.getCode())
                    .eq(query.getAddressId() != null, SysOrgan::getAddressId, query.getAddressId())
                    .like(StringUtils.isNotBlank(query.getLeaderPhone()), SysOrgan::getLeaderPhone, query.getLeaderPhone());


        }
        return wrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<SysOrgan> queryInfoWrapper() {


        MyLambdaQueryWrapper<SysOrgan> wrapper = new MyLambdaQueryWrapper<>();

        wrapper.select(SysOrgan::getId, SysOrgan::getName, SysOrgan::getName, SysOrgan::getType,
                SysOrgan::getCode, SysOrgan::getAddress, SysOrgan::getLeader, SysOrgan::getLeaderPhone, SysOrgan::getAddressId,

                SysOrgan::getStatus, SysOrgan::getCreateDate, SysOrgan::getUpdateDate,SysOrgan::getLicense,SysOrgan::getFullName,SysOrgan::getIcon, SysOrgan::getAddress);
        wrapper.imageUrl(SysOrgan::getIcon).as("icon_url").addressName(SysOrgan::getAddressId).as("address_name")
        .imageUrl(SysOrgan::getLicense).as("license_url");

        return wrapper;
    }

    @Operation(summary = "获取机构简易列表")
    @GetMapping("/mini_list")
    public Result<List<SysOrgan>> miniList(
       @RequestParam(defaultValue = "0") int type

    ) {
        MyLambdaQueryWrapper<SysOrgan> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysOrgan::getId,  SysOrgan::getName);


            wrapper.eq(type != 0, SysOrgan::getType, type);


        List<SysOrgan> list = getBaseService().list(wrapper);
        return list.isEmpty() ? Result.error(404,"暂无数据") : Result.ok(list);
    }

}
